package com.lwp.htmlparse;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.apache.commons.lang3.time.DateUtils;

import java.io.IOException;
import java.net.MalformedURLException;

/**
 * HtmlunitTest
 *
 * @author xxx
 */
public class HtmlunitTest {

    // Disabling JavaScript support: to disable JavaScript processing for one WebClient, call webClient.getOptions().setJavaScriptEnabled(false).
    public HtmlPage getHtmlPageResponse(WebClient webClient, String url) throws Exception {

        int timeout = 30_000;
        int waitForBackgroundJavaScript = timeout;

        webClient.getOptions().setThrowExceptionOnScriptError(false);//当JS执行出错的时候是否抛出异常
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);//当HTTP的状态非200时是否抛出异常
        webClient.getOptions().setActiveXNative(false);
        webClient.getOptions().setCssEnabled(true);//是否启用CSS
        webClient.getOptions().setJavaScriptEnabled(true); //很重要，启用JS
        webClient.getOptions().setRedirectEnabled(true);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要，设置支持AJAX

        webClient.getCookieManager().setCookiesEnabled(true);

        webClient.getOptions().setTimeout(timeout);//设置“浏览器”的请求超时时间
        webClient.setJavaScriptTimeout(timeout);//设置JS执行的超时时间

        HtmlPage page;
        try {
            page = webClient.getPage(url);
        } catch (Exception e) {
            webClient.close();
            throw e;
        }
        webClient.waitForBackgroundJavaScript(waitForBackgroundJavaScript);//该方法阻塞线程

        return page;

    }


    public static void main(String[] args) {
        // "applicationVersion":"5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
        /*WebClient webClient = new WebClient();
        System.out.println(webClient.getBrowserVersion());
        webClient.close();*/
//        System.out.println(JSON.toJSONString(webClient, true));
       /* HtmlPage htmlPage = httputils.getHtmlPageResponse(webClient, infoSource.getSourceUrl());
        Document document = Jsoup.parse(htmlPage.asXml());//获取html文档*/

        try (final WebClient webClient = new WebClient()) {
//            final HtmlPage page = webClient.getPage("https://576969.com/");
            final HtmlPage page = webClient.getPage("https://576969.com/bbs/amcz.js");
//            final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
            System.out.println(page.getTitleText());
            System.out.println(page.asXml());
            System.out.println(page.asText());

            /*final String pageAsXml = page.asXml();
            Assert.assertTrue(pageAsXml.contains("<body class=\"composite\">"));

            final String pageAsText = page.asText();
            Assert.assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols"));*/
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

//        DateUtil.endOfYear()


       /* try (final WebClient webClient = new WebClient()) {
            final HtmlPage page = webClient.getPage("https://576969.com/");
//            final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
            Assert.assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

            final String pageAsXml = page.asXml();
            Assert.assertTrue(pageAsXml.contains("<body class=\"composite\">"));

            final String pageAsText = page.asText();
            Assert.assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

    }

    public static String replaceStr(String s) {

        s = s.replace("\n\r", "<br>&nbsp;&nbsp;");
        s = s.replace("\r\n", "<br>&nbsp;&nbsp;");//这才是正确的！
        s= s.replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
        s =s.replace(" ", "&nbsp;");
        s=s.replace("\"", "\\"+"\"");//如果原文含有双引号，这一句最关键！！！！！！

        return s;
    }
}
